package com.xx.menu;

import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;

public class AddMenu {
    private final MenuBar mb;
    private Menu m3;
    private Menu m1;
    private Menu m2;
    private Menu m4;

    public AddMenu(StackPane root) {
        //        菜单栏,width与底层pane的width绑定.
        mb = new MenuBar();
        root.widthProperty().addListener((ob, ov, nv) -> mb.setPrefWidth((Double) nv));
//        m1.setOnShowing(e-> System.out.println("showing"));
//        m1.setOnShown(e-> System.out.println("shown"));
//        都是展示内容时调用.
//        m1.setOnHiding(e-> System.out.println("hiding"));
//        m1.setOnHidden(e-> System.out.println("hidden"));
//        都是关闭内容时间调用.调用顺序为:ing->n.menu的setOnAction方法只有在他具有item时才有效.
//        menuItem 是 menu的父类.
        menu4();
        menu3();
        menu2();
        menu1();
        mb.getMenus().addAll(m1, m2, m3, m4);

        root.getChildren().add(mb);
    }

    private void menu4() {
        Menu m4 = new Menu("menu4");
        this.m4 = m4;
    }

    private void menu3() {
        Menu m3 = new Menu("menu3");
        //        多选框
        CheckMenuItem cmi1 = new CheckMenuItem("checkMenuItem1");
        cmi1.setOnAction(e -> System.out.println("click cmi1"));
        cmi1.setSelected(true);
        cmi1.setDisable(true);
        CheckMenuItem cmi2 = new CheckMenuItem("checkMenuItem2");
        CheckMenuItem cmi3 = new CheckMenuItem("checkMenuItem3");
        CheckMenuItem cmi4 = new CheckMenuItem("checkMenuItem4");
        m3.getItems().addAll(cmi1, cmi2, cmi3, cmi4);

        this.m3 = m3;
    }

    private void menu2() {
        Menu m2 = new Menu("menu2");
        //        单选框
        ToggleGroup tg = new ToggleGroup();
        RadioMenuItem rm1 = new RadioMenuItem("radioMenuItem1");
        rm1.setToggleGroup(tg);
        rm1.setOnAction(e -> System.out.println("click radioMenu item 1"));
        rm1.setSelected(true);
        RadioMenuItem rm2 = new RadioMenuItem("radioMenuItem2");
        rm2.setToggleGroup(tg);
        RadioMenuItem rm3 = new RadioMenuItem("radioMenuItem3");
        rm3.setToggleGroup(tg);
        RadioMenuItem rm4 = new RadioMenuItem("radioMenuItem4");
        rm4.setToggleGroup(tg);
        m2.getItems().addAll(rm1, new SeparatorMenuItem(), rm2, new SeparatorMenuItem(), rm3, new SeparatorMenuItem(),
                m4);
        this.m2 = m2;
    }

    private void menu1() {
        Menu m1 = new Menu("菜单");
        ImageView imageView = new ImageView("https://img0.baidu.com/it/u=3788269870," +
                "3253610096&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
        imageView.setFitWidth(20);
        imageView.setFitHeight(20);
        m1.setGraphic(imageView);

        MenuItem item1 = new Menu("item1");
        item1.setOnAction(e -> System.out.println("点击了item1"));
        MenuItem item2 = new Menu("item2");
        item2.setOnAction(e -> System.out.println("点击了item2"));
        MenuItem item3 = new Menu("item3");
        item3.setOnAction(e -> System.out.println("点击了item3"));
        MenuItem item4 = new Menu("item4");
        item4.setOnAction(e -> System.out.println("点击了item4"));
        MenuItem item5 = new Menu("item5");
        item5.setOnAction(e -> System.out.println("点击了item5"));
        m1.getItems().addAll(item1, new SeparatorMenuItem(), m2, item2, new SeparatorMenuItem(), item3,
                new SeparatorMenuItem(), item4, new SeparatorMenuItem(),
                item5);

        this.m1 = m1;
    }
}
